qui(
    function (require, exports, module) {
        //插入视频管理
        WrapSys.ShimManager = (function () {
            var entityIdIndex = 1;
            var pageBeanSource = WrapSys.Util.createPageBean();
            var operator = {
                add: function (sourceEntity) {
                    var entity = createEntity(sourceEntity)
                    pageBeanSource.addNewOne(entity);
                    operator.notifyUpdateContentView(entity)
                },
                del: function (spanElement) {
                    var shimDiv = $(spanElement).parent();
                    var entityId = parseInt($(spanElement).attr("sourceId"));
                    var findData = pageBeanSource.findData("id", entityId);
                    if (findData != null) {
                        var handler = function () {
                            shimDiv.remove();
                            pageBeanSource.delOne(findData)
                            console.log(" WrapSys.ShimManager del " + entityId + "|" + (findData != null))
                        }
                        WrapSys.Util.dataChangeRemindCheck(findData, handler);
                    }
                },
                modify: function (entityId, options) {
                    var entity = WrapSys.ShimManager.getEntity(entityId);
                    for (var key in options) {
                        if (key in  entity) {
                            entity[key] = options[key];
                        }
                    }
                    if (entity.insertFrameId != -1) {
                        var dataArr = getSource();
                        var sameArr = [];
                        $.each(dataArr, function (i, elmt) {
                            if (elmt.relationPic == entity.relationPic) {
                                sameArr.push(elmt);
                            }
                        })
                        if (sameArr.length > 1) {
                            sameArr.sort(WrapSys.Config.SORT_RULE.asc_frameId_entityId);
                            sameArr.reverse();
                        }
                        $.each(sameArr, function (i, node) {
                            $("span[sourceId=" + node.id + "]").parent().insertAfter($("#img-" + node.relationPic).parent())
                        })
                    }
                },
                notifyUpdateContentView: function (entity) {
                    var global = WrapSys.Config.Global;
                    var style1 = global.smallPic ? "width:120px;height:96px" : "width:280px;height:220px";
                    var style2 = global.smallPic ? WrapSys.Config.SMALL_PIC_MODE : WrapSys.Config.BIG_PIC_MODE;
                    var spanCls = global.smallPic ? "shim_time" : "shim_time2";
                    var htmlArr = [];
                    htmlArr.push('<div class="text-center relative shim-img-unselected" shim-key="true" shim-father="' + entity.relationPic + '" style="' + style1 + '">');
                    htmlArr.push('<img class="shim-img" ' + style2 + ' src="' + entity.imgUrl + '"/>');
                    htmlArr.push('<div class="absolute ' + spanCls + '">' + accountPlayTime(entity) + '</div><br/>');
                    htmlArr.push('<span class="shim" sourceId="' + entity.id + '" onclick="WrapSys.ShimManager.delEntity(this)">删除</span>');
                    htmlArr.push('<label style="position: absolute;top: 0px;left: 0px;word-break: break-all; width: 100%;pointer-events: none;text-align: left;background-color: grey">' + entity.contentType + '</label>');
                    htmlArr.push('<label name="short_name" style="position: absolute;top: 30px;left: 0px;word-break: break-all; width: 100%;pointer-events: none;display: none">' + formatShowName(entity.name) + '</label>');
                    htmlArr.push('<label name="full_name" style="position: absolute;top: 45%;left: 7px;word-break: break-all; width: 94%;pointer-events: none;display: none">' + entity.name+ '</label>');
                    htmlArr.push('</div>');
                    this.insertControl(htmlArr.join(""), entity);
                    $("span[sourceId=" + entity.id + "]").parent().find("img").click();
                    $("span[sourceId=" + entity.id + "]").parent().find("label[name="+(global.smallPic?"short_name":"full_name")   +"]").show();
                    WrapSys.AccurateFrame.handler_keyCodeQ_shim();
                },
                notifySortView: function (entity) {
                    var shimArr = $("div[shim-father=" + entity.relationPic + "]");
                },
                insertControl: function (html, entity) {
                    var shimArr = $("div[shim-father=" + entity.relationPic + "]");
                    if (shimArr.length == 0) {
                        $(html).insertAfter($("#img-" + entity.relationPic).parent());
                    } else {
                        $(html).insertAfter($(shimArr[shimArr.length - 1]));
                    }
                },
                clear: function () {
                    var dataArr = getSource();
                    if (dataArr.length > 0) {
                        var hasReadyToSend = false;
                        var handler = function () {
                            var waitToDeal = [];
                            $.each(dataArr, function (i, element) {
                                if (waitToDeal.indexOf(element.relationPic) == -1) {
                                    waitToDeal.push(element.relationPic)
                                }
                                if (element.readyToSend == true) {
                                    hasReadyToSend = true;
                                }
                            })
                            $.each(waitToDeal, function (i, shimFather) {
                                $('div[shim-father="' + shimFather + '"]').remove();
                            })
                            pageBeanSource.setNewData([])
                        }
                        WrapSys.Util.dataChangeRemindCheck({readyToSend: hasReadyToSend}, handler);
                    }
                }
            }

            function formatShowName(name) {
                if( name.length * 7 + 15 > 120 ){
                    return name.substring(0,15)+"...";
                }
                return name
            }

            function createEntity(original) {
                var shimEntity = {
                    id: entityIdIndex,
                    materialId: original.id,
                    name: original.name,
                    fpTime: original.time,
                    imgUrl: original.url,
                    relationPic: parseInt($(".red").parent().find(".pid").html()),
                    contentType: original.contentType,
                    insertFrameId: -1
                }
                entityIdIndex++;
                return shimEntity
            }

            function accountPlayTime(element) {
                return   WrapSys.Evt.transToFpTime(element.fpTime);
            }

            function getEntity(sourceId) {
                var entity = pageBeanSource.findData("id", sourceId);
                return entity;
            }

            function getSource() {
                return pageBeanSource.getSource();
            }

            function hideShim(picBegin, picEnd) {
                var dataArr = getSource();
                var waitToDeal = [];
                $.each(dataArr, function (i, element) {
                    if (element.relationPic >= picBegin && element.relationPic <= picEnd) {
                        if (waitToDeal.indexOf(element.relationPic) == -1) {
                            waitToDeal.push(element.relationPic)
                        }
                    }
                })
                $.each(waitToDeal, function (i, shimFather) {
                    $('div[shim-father="' + shimFather + '"]').hide();
                })
            }

            function showShim(picBegin, picEnd) {
                var dataArr = getSource();
                var waitToDeal = [];
                $.each(dataArr, function (i, element) {
                    if (element.relationPic >= picBegin && element.relationPic <= picEnd) {
                        if (waitToDeal.indexOf(element.relationPic) == -1) {
                            waitToDeal.push(element.relationPic)
                        }
                    }
                })
                $.each(waitToDeal, function (i, shimFather) {
                    $('div[shim-father="' + shimFather + '"]').show();
                })
            }

            function getAdAddList(limitStart, limitEnd, readyToSend) {
                //{“material_id”:123,"id":5,"offset":1,""}
                limitStart = parseInt(limitStart);
                limitEnd = parseInt(limitEnd);
                var ret = [];
                var source = getSource();
                $.each(source, function (i, elemt) {
                    if (elemt.relationPic < limitStart || elemt.relationPic > limitEnd) {
                        return true;
                    }
                    ret.push({material_id: elemt.materialId, id: elemt.relationPic, offset: elemt.insertFrameId == -1 ? "" : elemt.insertFrameId, uuid: elemt.id + ""});
                    if (readyToSend) {
                        elemt["readyToSend"] = true;
                    }
                })
                ret.sort(WrapSys.Config.SORT_RULE.asc_news_sTime)
                return ret;
            }

            function checkHasAllSetFrame() {
                var source = getSource();
                var hasAllSetFrame = true;
                $.each(source, function (i, elemt) {
                    if (elemt.insertFrameId == -1) {
                        hasAllSetFrame = false;
                        return false;
                    }
                })
                return hasAllSetFrame
            }

            function checkRangeHasAllSetFrame(limitStart, limitEnd) {
                limitStart = parseInt(limitStart);
                limitEnd = parseInt(limitEnd);
                var source = getSource();
                var hasAllSetFrame = true;
                $.each(source, function (i, elemt) {
                    if (elemt.relationPic < limitStart || elemt.relationPic > limitEnd) {
                        return true;
                    }
                    if (elemt.insertFrameId == -1) {
                        hasAllSetFrame = false;
                        return false;
                    }
                })
                return hasAllSetFrame
            }

            function registerEvent() {
                VODCom.Pic.eventManager.bindEvent("fold_pic", function (startIndex, endIndex) {
                    hideShim(startIndex, endIndex)
                })
                VODCom.Pic.eventManager.bindEvent("extend_pics", function (startIndex, endIndex) {
                    showShim(startIndex, endIndex)
                })

                VODCom.Pic.eventManager.bindEvent("change_big_pic",function(){
                    $('div[shim-key]').css({
                        'width': '280px',
                        'height': '220px'
                    });
                    $('div[shim-key]').find("label[name=full_name]").show();
                    $('div[shim-key]').find("label[name=short_name]").hide();
                    $('.shim_time').removeClass('shim_time').addClass('shim_time2');
                })
                VODCom.Pic.eventManager.bindEvent("change_small_pic",function(){
                    $('div[shim-key]').css({
                        'width': '120px',
                        'height': '96px'
                    });
                    $('div[shim-key]').find("label[name=full_name]").hide();
                    $('div[shim-key]').find("label[name=short_name]").show();
                    $('.shim_time2').removeClass('shim_time2').addClass('shim_time');
                })
            }

            return {
                addEntity: operator.add,
                modifyEntity: operator.modify,
                clearEntity: operator.clear,
                delEntity: operator.del,
                getEntity: getEntity,
                getSource: getSource,
                getAdAddList: getAdAddList,
                checkHasAllSetFrame: checkHasAllSetFrame,
                checkRangeHasAllSetFrame: checkRangeHasAllSetFrame,
                registerEvent: registerEvent
            }
        })();
    }
);